package com.dankegongyu.shell.js.common;

import android.content.Context;
import androidx.annotation.LayoutRes;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Emmanuel on 2017/10/31.
 */
public class SimpleRecycleAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    /**
     * 上下文
     */
    private Context mContext;

    /**
     * 数据
     */
    private List<T> mData;

    /**
     * 布局资源
     */
    private int mLayoutId;

    /**
     * 额外数据
     */
    private Object[] mExtras;

    /**
     * 单选索引
     */
    private int radioPosition = 0;

    /**
     * 多选索引
     */
    private HashMap<Integer, Boolean> multiPosition = new HashMap<>();

    /**
     * 点击事件
     */
    private IOnItemClickListener mItemClickListener = null;

    /**
     * 长点击时间
     */
    private IOnItemLongClickListener mIOnItemLongClickListener=null;

    /**
     * @param context  上下文
     * @param data     数据
     * @param layoutId 布局资源
     */
    public SimpleRecycleAdapter(Context context, List<T> data, @LayoutRes int layoutId) {
        this.mContext = context;
        if (data == null) {
            data = new ArrayList<>();
        }
        this.mData = data;
        this.mLayoutId = layoutId;
    }

    /**
     * 获取列表数据
     *
     * @return
     */
    public List<T> getData() {
        return mData;
    }

    /**
     * 设置列表数据
     *
     * @return
     */
    public void setData(List<T> data) {
        if (data == null) {
            data = new ArrayList<>();
        }
        this.mData = data;
    }

    /**
     * 额外数据的获取
     *
     * @return
     */
    public Object[] getExtras() {
        return mExtras;
    }

    /**
     * 额外数据的设置
     *
     * @param data
     */
    public void setExtras(Object... data) {
        this.mExtras = data;
    }


    /**
     * 获取单选索引设置
     *
     * @return
     */
    public int getRadioPosition() {
        return radioPosition;
    }

    /**
     * 设置单选索引
     *
     * @param radioPosition
     */
    public void setRadioPosition(int radioPosition) {
        this.radioPosition = radioPosition;
    }


    /**
     * 获取多选索引设置
     *
     * @return
     */
    public HashMap<Integer, Boolean> getMultiPosition() {
        return multiPosition;
    }

    /**
     * 设置单选索引
     */
    public void setMultiPosition(int position, boolean choice) {
        multiPosition.put(position, choice);
    }

    /**
     * 清空单选索引
     */
    public void clearMultiPosition() {
        multiPosition.clear();
    }


    /**
     * item 点击事件
     *
     * @param listener
     */
    public void setOnItemClickListener(IOnItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    /**
     * item 长点击事件
     * @param listener
     */
    public void setOnItemLongClickListener(IOnItemLongClickListener listener){
        this.mIOnItemLongClickListener=listener;
    }

    @Override
    public int getItemCount() {
        if (mData == null) {
            return 0;
        }
        return mData.size();
    }

    public T getItem(int position) {
        if (mData == null || position >= mData.size()) {
            return null;
        }
        return mData.get(position);
    }

    /**
     * create viewholder
     *
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(mLayoutId, parent, false);
        RecyclerView.ViewHolder holder = new ViewHolder(view);
        return holder;

    }

    /**
     * bind ViewHolder
     *
     * @param h
     * @param position
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, final int position) {

        ViewHolder holder = (ViewHolder) h;
        holder.itemView.setTag(position);
        if (holder.itemView instanceof IRecycleViewListener) {
            ((IRecycleViewListener) holder.itemView).setData(getItem(position), position, this);
        }

    }

    /**
     * ViewHolder
     */
    public class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(final View v) {
            super(v);

            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mItemClickListener != null) {
                        mItemClickListener.onItemClick(v, (int) v.getTag());
                    }
                }
            });

            v.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if(mIOnItemLongClickListener!=null){
                        mIOnItemLongClickListener.onLongItemClick(v,(int)v.getTag());
                    }
                    return false;
                }
            });

        }
    }


}
